﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace NTTDATA.BEENChina.ResultCertification.PropertyGridUtil
{
    public class CustomPropertyDescriptor<T> : PropertyDescriptor where T : PropertyData
    {
        private CustomCollection<T> collection = null;
        private int index = -1;

        public CustomPropertyDescriptor(CustomCollection<T> coll, int idx) :
            base("#" + idx.ToString(), null)
        {
            this.collection = coll;
            this.index = idx;
        }

        public override AttributeCollection Attributes
        {
            get
            {
                return new AttributeCollection(null);
            }
        }

        public override bool CanResetValue(object component)
        {
            return true;
        }

        public override Type ComponentType
        {
            get
            {
                return this.collection.GetType();
            }
        }

        public override string DisplayName
        {
            get
            {
                if (index >= this.collection.Count())
                    return "";
                PropertyData pair = this.collection[index];
                if (pair == null) return "";
                return pair.GetDisplayName();
            }
        }

        public override string Description
        {            
            get
            {
                if (index >= this.collection.Count)
                    return "";
                PropertyData pair = this.collection[index];
                if (pair == null) return "";
                return pair.GetDescription();
            }
        }

        public override object GetValue(object component)
        {
            if (index >= this.collection.Count)
                return null;
            return this.collection[index];
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override string Name
        {
            get { return "#" + index.ToString(); }
        }

        public override Type PropertyType
        {

            get 
            {
                if (index >= this.collection.Count)
                    return null;

                return this.collection[index].GetType(); 
            }
        }

        public override void ResetValue(object component)
        {
        }

        public override bool ShouldSerializeValue(object component)
        {
            return true;
        }

        public override void SetValue(object component, object value)
        {
            //this.collection[index] = value;
        }
    }
}